import api from '../../../utils/api'
import message from '../../../utils/message'

// pages/order/refund/detail.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        refund_detail: null,
        has_load: false,
        is_send_back:false,
        express_no: '',
        express_name: ''

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        console.log(options)

        if(options.id){
            this.getRefundDetail(options.id)
        }

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    getRefundDetail:function (refund_id) {
        var me = this;
        api.refund.getRefundDetail( {refund_id:refund_id} , function (response) {

            console.log(response);
            if(response.status==1){

                me.setData({
                    has_load:true,
                    refund_detail:response.data
                })
                
            }
            
        } )
        
    },
    doSendBack:function (e) {

        var action = e.currentTarget.dataset.action;
        if(action==1){
            this.setData({
                is_send_back: true,
                express_no:'',
                express_name:''
            })
        }

        if(action==-1){
            this.setData({
                is_send_back: false,
                express_no:'',
                express_name:''

            })
        }

        if(action==2){

            var data = {}
            data.express_name = this.data.express_name;
            data.express_no = this.data.express_no;
            data.refund_id = this.data.refund_detail.id;
            api.refund.saveSendBackInfo( data , function (response) {
                if(response.status==1){
                    message.show_error(response.message );
                }else{
                    message.show_error( response.message )
                }
                
            })
        }
        
    },
    doScanCode:function (e) {
        var me = this;
        var action=e.currentTarget.dataset.action;

        if(action==1){
            wx.scanCode({
              onlyFromCamera: true,
              scanType: ['barCode' ,'qrCode'],
              success:function (res) {
                  console.log(res)

                  if(res.result){
                      me.setData({
                          express_no: res.result
                      })
                  }else{
                      wx.showToast({
                        title: '扫码失败',
                      })
                  }
                  
              },
              fail:function (res) {
                  console.log(res)
                  
              },
              complete:function(){

              }
            })
        }
        
    },
    doInput:function (e) {
        var name = e.currentTarget.dataset.name;
        
        var t = {}
        t[name] = e.detail.value;
        

        this.setData( t );
    },
    previewImage:function (e) {
        var arr = [];
        arr.push( e.currentTarget.dataset.src );
        wx.previewImage({
          urls: arr,
        })
        
    }
})